//
//  MikerTabListView.swift
//  MikerShop
//
//  Created by eme on 2016/12/12.
//  Copyright © 2016年 eme. All rights reserved.
//

import Foundation
import SnapKit
import UtilCore

let MikerTabSize = 100

protocol MikerTabListViewDataSource : NSObjectProtocol{
    
    func mikerTabListView(mikerTabListView: MikerTabListView, cellForRowAtIndex index: NSInteger) -> MikerItemCell

    
    func mikerTabListView(mikerTabListView: MikerTabListView, updateRowAtIndex index: NSInteger) -> String
    
    func numberOfMikerTabListView(mikerTabListView: MikerTabListView) -> Int
}
protocol MikerTabListViewDelegate : NSObjectProtocol, UIScrollViewDelegate{
    
     func mikerTabListView(mikerTabListView: MikerTabListView, didSelectRowAtIndex index: NSInteger)
}


class MikerTabListView: UIScrollView {
    
    
     var dataSource: MikerTabListViewDataSource?
     var mikerdelegate: MikerTabListViewDelegate?
    
    private var itemCells:[MikerItemCell] = []
    private var previousView:MikerItemCell?
    private var lastView:MikerItemCell?
    private var lastIndex:NSInteger?
    private var currentView:MikerItemCell?
    func setupContentView() {
        guard let  dataCount = self.dataSource?.numberOfMikerTabListView(mikerTabListView: self) else {
            return
        }
        for i in 0...dataCount - 1{
            guard let view = self.dataSource?.mikerTabListView(mikerTabListView: self, cellForRowAtIndex: i)else {
                return
            }
            view.tag = i+MikerTabSize
            view.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(MikerTabListView.channelLabelClick(_:))))
            view.isUserInteractionEnabled = true
          
            self.addSubview(view)
            self.itemCells.append(view)
            if self.previousView == nil {
                view.hasSelected = true
                self.lastIndex = i+MikerTabSize
                view.snp.makeConstraints { make in
                    make.centerY.equalToSuperview()
                    make.top.equalTo(0)
                    make.leading.equalTo(0)
                }
                
            }else if i == dataCount - 1{
                view.hasSelected = false
                view.snp.makeConstraints { make in
                    make.top.equalTo(0)
                    make.leading.equalTo(self.previousView!.snp.trailing).offset(0)
                    make.centerY.equalToSuperview()
                    make.trailing.equalTo(0)
                    
                }
            }else{
                view.hasSelected = false
                view.snp.makeConstraints { make in
                    make.top.equalTo(0)
                    make.centerY.equalToSuperview()
    
                    make.leading.equalTo(self.previousView!.snp.trailing).offset(0)
                    
                }
            }
            self.previousView = view
            
        }
       
    
    }
    //这个方法提供给 HomeViewController
    func setBottomView(btnIndex:Int)
    {
        //将其他itemCell的selected状态设置为 false
        for  view in self.subviews{
           
            if( view.isKind(of: MikerItemCell.self) && view.tag != btnIndex+MikerTabSize){
                (view as! MikerItemCell).hasSelected = false
            }
        }
        let itemCell = self.viewWithTag(btnIndex+MikerTabSize) as? MikerItemCell
        itemCell?.hasSelected = true
        self.lastView = itemCell
        UIView.animate(withDuration: 0.3, animations: { () -> Void in
            self.updateScrollViewContentOffset(itemCell: itemCell!)
        })
    }
    /// 更新contentOffset
    func updateScrollViewContentOffset(itemCell:MikerItemCell) -> Void {
        let buttonX = itemCell.frame.origin.x
        let buttonWidth = itemCell.frame.size.width
        let scrollerWidth = self.contentSize.width
        // 移动到中间
        if (scrollerWidth > screen_Width && // Scroller的宽度大于屏幕宽度
            buttonX > screen_Width / 2.0 - buttonWidth / 2.0 && //按钮的坐标大于屏幕中间位置
            scrollerWidth > buttonX + screen_Width / 2.0 + buttonWidth / 2.0 // Scroller的宽度大于按钮移动到中间坐标加上屏幕一半宽度加上按钮一半宽度
            ) {
            
            self.contentOffset = CGPoint(x: buttonX - screen_Width / 2.0 + buttonWidth / 2.0, y: 0);
        } else if (buttonX < screen_Width / 2.0 - buttonWidth / 2.0) { // 移动到开始
            self.contentOffset = CGPoint(x: 0, y: 0);
        } else if (scrollerWidth - buttonX < screen_Width / 2.0 + buttonWidth / 2.0 || // Scroller的宽度减去按钮的坐标小于屏幕的一半，移动到最后
            buttonX + buttonWidth + 20.0 == scrollerWidth) {
            if (scrollerWidth > screen_Width) {
                self.contentOffset = CGPoint(x: scrollerWidth - screen_Width, y: 0); // 移动到末尾
            }
        }
    }
    
    /**
     频道label的点击手势回调方法，
     当点击事件发生后，将新闻列表切换到被点击label对应的新闻列表
     */
    func channelLabelClick(_ recognizer: UITapGestureRecognizer) {
        let itemCell = recognizer.view as! MikerItemCell
        let index = itemCell.tag
        
        //将其他itemCell的selected状态设置为 false
        for  view in self.subviews{
            if(view.isKind(of: MikerItemCell.self) && view.tag != index+MikerTabSize){
                (view as! MikerItemCell).hasSelected = false
            }
        }
        itemCell.hasSelected = true
        self.mikerdelegate?.mikerTabListView(mikerTabListView: self, didSelectRowAtIndex: index)
        UIView.animate(withDuration: 0.3, animations: { () -> Void in
            self.updateScrollViewContentOffset(itemCell: itemCell)
        })
        self.lastIndex = index
    }
    
    func reloadData() -> Void {
        guard let  dataSource = self.dataSource else{
            return
        }
        for (index, element) in self.itemCells.enumerated(){
            element.header_Lb.text = dataSource.mikerTabListView(mikerTabListView: self, updateRowAtIndex: index)
        }
    }
    
}




